package practise.hibernate.Service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import practise.hibernate.Service.AddressService;
import practise.hibernate.dao.AddressDao;
import practise.hibernate.dao.PersonDao;
import practise.hibernate.persistence.Address;

import java.util.List;

@Service("addressService")
@Repository
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    @Autowired
    private PersonDao personDao;

    @Override
    @Transactional(readOnly = true)
    public List<Address> findAllAddresses() {
        return addressDao.findAllAddresses();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Address> findPersonByName(String location) {
        return addressDao.findByLocation(location);
    }

    @Override
    @Transactional(readOnly = false)
    public void deleteAddress(int id){
           addressDao.deleteAddress(id);
    }
    @Override
    @Transactional(readOnly = false)
    public Address updateAddressStreet(String location, String street){
        List<Address> addressList =  addressDao.findByLocation(location);
        Address addressUpdating = null;
        for(Address address : addressList){
            if(address.getLocation().equals(location)){
                addressUpdating = address;
                break;
            }

        }
        addressUpdating.setStreet(street);

        return addressUpdating;
    }
    @Override
    @Transactional(readOnly = false)
    public Address addAddress(Address address){
        return addressDao.merge(address);

    }


}
